#define LINE_LEN 16
#define BAUD 115200
#define NEWLINE 10

char next_byte; // the byte just read
char message[LINE_LEN];	// for incoming serial data
int i = 0; // counter for message index
int relay_pin = 11; // pin connected to relay
int led_pin = 12;
int ser_avail = 0; // bytes available

void setup() {
  Serial.begin(BAUD);	// opens serial port
  pinMode(relay_pin, OUTPUT);
}

void loop() {
  //digitalWrite(relay_pin, HIGH);
  //int i = 0;
  // send data only when you receive data:
  while(1) {
    if (Serial.available()){
      message[i] = Serial.read();
      i++;
    }
    if (message[i-1] == NEWLINE) {
      break;
    }
  }
  /*
  for (int j=0; j < i; j++){
    delay(10);
    Serial.print(message[j]);
  }
  */
  if (message[0] == 'o') {
    Serial.print("On\n");
    digitalWrite(relay_pin, HIGH);
    delay(100);
  } else if (message[0] == 'f') {
    Serial.print("Off\n");
    digitalWrite(relay_pin, LOW);
    delay(100);
  }
  //while (message[i] != '\n'){
    //if (Serial.available() > 0) {
    //  digitalWrite(relay_pin, HIGH);
    //  delay(100);
    //  digitalWrite(relay_pin, LOW);
    //  delay(100);
      // read the incoming byte:
    //  message[i++] = Serial.read();
      //if (message[i] == 'o') {
      //  digitalWrite(relay_pin, HIGH);
      //} else if (message[i] == 'f') {
      //  digitalWrite(relay_pin, LOW);
      //}
      // say what you got:
    //}
  //}
  i = 0;
}
